/**
 * @fileOverview This is a group of utility functions that are needed in
 * different areas of projects.
 * @version 2.0
 * @author Joe Cianflone
 * @copyright CC, Creative Commons Attribution-ShareAlike 3.0 Unported -- http://creativecommons.org/licenses/by-sa/3.0/
 */

/**
 * Need to use a copyright date somewhere?  Just call this function and you get 
 * a dynamic year.  
 * @param {String} loc the node you want to drop the copyright date into
 * @param {String} statement anything else you want to add in right after the date
 */
function copyright (loc, statement) {
	var d = new Date();
	
	var childNode = document.createTextNode(d.getFullYear()+ " "+statement);
	$(loc).appendChild(childNode);
}//copyright()...


/**
 * This preloads images to help speed up images appearing on pages.
 * @param {String} dir directory the images are located in on your sever
 * @param {Object} loaders[] names of the images you want to load
 */
function preloader (dir, loaders) {
	var photo = new Array();
	for (var i=0, len = loaders.length; i <= len; ++i) {
		photo[i] = new Image();
		photo[i].src = dir+loaders[i];
	}//for...	
}//preloader()...

/**
 * Uses Script.aculo.us effects to toggle a child element open and closed.  
 * @param {Object} evt this is an object that represents the event that occured and the element it occured on NOTE: prototype event observers automatically pass this parameter.  You don't need to create it anywhere.
 */
function toggleChild (evt) {
	var element = Event.element(evt);
	var toggler = $(element.id).next();
	if (toggler == null) {
		toggler = $(element.id).up();
		toggler = toggler.next();
	}//if...
	Effect.toggle(toggler, 'blind', {duration:1});
}//toggleChild()...

/**
 * The XHTML 1.0 spec does not allow for target='new' events.  This function
 * remedies that.
 */
function externalLinks() {
	try {
		var anchors = document.getElementsByTagName("a");
		for (var i=0, len=anchors.length; i < len; ++i) {
			if (anchors[i].getAttribute("rel") == "external") {
				anchors[i].target = "_blank";
			}//if...
		}//for...
	}//try...
	catch (e) {
		return 0;
	}//catch...
}//externalLinks()...

/**
 * A more elegant wrapper class for SWFObject.  Makes loading multiple movies with
 * multiple parameters easier and helps to encapsulate the SWFObject code.  
 * @param {Object} params required parameters that SWFObject needs to work
 * @param {Object} variables optional values the user defines and passes into the flash movie as needed
 * @param {Object} divID the HTML ID that the movie should appear in defaults to flashcontent if divID is null
 * @example Event.observe(window, 'load', function(){
 * startMovie(
 * 	{
 * 		src: '../url/to/movie.swf',		//location of your movie
 * 		name: 'Movie Name',				//name of your movie
 * 		width: '640',					//width
 * 		height: '480',					//height
 * 		version: '8',					//lowest compatabile version of flash you want to work with
 * 		bgcolor: '#fff'					//background color if you movie doesnt fill the div
 * 		}, 
 * 		{
 * 			myVar: 'something I am setting'	//variable that is needed in the flash movie
 * 		}, 
 * 		'movieDIV'); 					//over riding the flashcontent DIV name
 * }, false);
 * @exception startMovie Exception
 */
function startMovie (params, variables, divID) {
	try {
		var so = new SWFObject(params.src, params.name, params.width, params.height, params.version, params.bgColor);
		try {
			variables = $H(variables);
			variables.each(function(pairs) {
				so.addVariable(pairs.key.toString(), pairs.value);
			});//url.each()...
		}//try...
		catch (e) {}//catch...
		try {
			so.write(divID);	
		}//try...
		catch (e) {
			divID = 'flashcontent';
			so.write(divID);	
		}//catch...
	}//try...
	catch (e) {
		alert (e+" startMovie Exception");
	}//catch...
}//startMovie()...
/**
 * 
 * @param {Event} e
 */
function submit(e) {
	var url ="";
	
	new Ajax.Request(url, {
			method: 'post',
			onSuccess: readMessage,
			onFailure: fallback
		}
	);//Ajax.Request...
}//submit()...

/**
 * Grabs the returning data from an AJAX callback and converts that data into
 * an array that JavaScript can read and manipulate.  
 * @param {Object} transport
 * @param {String} rootNode
 * @return {Object} docRoot[]
 */
function toXMLData(transport, rootNode) {
	var response = transport.responseXML;
	var docRoot = response.getElementsByTagName(rootNode)[0];
	
	return $A(docRoot);
}//toXMLData()...

/**
 * 
 * @param {Object} transport
 */
function readMessage(transport) {

	var root = toXMLData(transport, 'nodeName');

}//readMessage()...

/**
 * 
 * @param {Object} transport
 */
function fallback(transport) {
	alert ("AJAX Callback Error!");
}//fallback()...
/**
 * 
 * @param {String} node
 * @param {String} parentTag
 * @param {String} childTag
 * @return {Object} results[]
 */
function parseChildren(node, parentTag, childTag) {
	var results = [];
	try {
		var children = $A(
			node.getElementsByTagName(parentTag)[0].getElementsByTagName(childTag)
		);
		results = children.collect (
			function(value, index) {
				return value.firstChild.data;
			}
		);
	}
	catch (e) {}
	
	return results;
}//parseChildren

/**
 * 
 */
function rollover () {
	var navigation = document.getElementsByClassName('rollover');
	for (var i = 0, len=navigation.length; i < len; ++i) {
		Event.observe(navigation[i], 'mouseover', over, false);
		Event.observe(navigation[i], 'mouseout', out, false);
	}//for...
}//rollover...3

/**
 * 
 * @param {Event} e
 */
function over (e) {
	var ele = Event.element(e);
	var parent = ele.up();
	var url = './ui/images/buttons/button_'+parent.id+'_hover.gif';
	ele.style.background = "transparent url("+url+") top left no-repeat";
}//over()...

/**
 * 
 * @param {Event} e
 */
function out(e) {
	var ele = Event.element(e);
	var parent = ele.up();
	var checkSelected = parent.id+'Selected';
	if (parent.className == 'selected') {
		var url = './ui/images/buttons/button_'+parent.id+'_selected.gif';	
	}//if...
	else {
		var url = './ui/images/buttons/button_'+parent.id+'.gif';		
	}//else...

	ele.style.background = "transparent url("+url+") top left no-repeat";
}//out()...
/**
 * 
 * @param {String} accordianGroup
 */
function loadAccordian (accordianGroup) {
	var accordianElements = document.getElementsByClassName(accordianGroup);
	
	for (var i=0, len=accordianElements.length; i < len; ++i) {
		Event.observe (accordianElements[i], 'click', accordian, false);
	}//for...
}//loadAccordian()...
/**
 * 
 * @param {Event} e
 */
function accordian (e) {
	var ele = Event.element(e);
	var openElement = document.getElementsByClassName('open');
	var oE = openElement[0];
	if (!oE) {
		new Effect.BlindDown (ele.next(), {Duration:'5'});	
	}//if...
	else {
		if (!ele.hasClassName('open')) {
			new Effect.Parallel(
				[new Effect.BlindUp(oE.next(), {Duration:'5'}), new Effect.BlindDown(ele.next(), {Duration:'5'})], 
				{
					afterFinish: function (e) {
						oE.removeClassName('open');
					}//afterFinish...
				}
			);//Effect.Parallel...
		}//if...
	}//else...
	ele.addClassName('open');
}//accordian()...